package data_structures_and_algorithms.tree.learn;

import java.util.List;

/**
 * @ClassName Code_zuo_god_maxHappy_in_employee
 * @Description: 员工最大快乐值
 *
 *
 * 题意描述：一个employee下可以有多个员工，最基层员工没有员工
 *
 * 举办一个晚宴，每个员工都有一个快乐值，如果邀请了A来，那么A的直接下属都不可以来，问员工最大快乐值是多少？
 * @Author: yongliang.ma
 * @Create_time:2022/10/30 22:01
 */
public class Code_zuo_god_maxHappy_in_employee {
    public class Employee{
        public int happy;
        public List<Employee> nexts;
        public Employee(int h){
            this.happy = h;
        }
    }

    public int maxHappy(Employee boss){
        if (boss == null)
            return 0;
        Info process = process(boss);
        return Math.max(process.yes, process.no);
    }

    public class Info{
        public int yes;// 来的情况下的最大快乐值
        public int no;// 不来的情况下的最大快乐值
        public Info(int yes, int no){
            this.yes = yes;
            this.no = no;
        }
    }

    public Info process(Employee employee){
        if (employee.nexts == null || employee.nexts.isEmpty())
            return new Info(employee.happy, 0);
        int yes = employee.happy;
        int no = 0;
        for (Employee next: employee.nexts) {
            Info info = process(next);
            yes += info.no;
            no += Math.max(info.yes, info.no);
        }

        return new Info(yes, no);
    }
}
